#include "main.h"
#include "math.h"

sterilization_type::sterilization_type(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::sterilization_type)
{
    ui->setupUi(this);
    manage=0;
    sr=new sterilizing_runing;
    m=NULL;
}

sterilization_type::~sterilization_type()
{
    delete sr;
    delete ui;
    if(m!=NULL) delete m;
}

void sterilization_type::on_manual_operation_btn_clicked()
{   
        BUTTON_SOUND;
    if(manage)
    {    
      user_add_process(false);
    }
        else
    {
     sterilization_protype= MANUAL_OPERATION;
     if(m==NULL)  m=new manual_operation;
     m->entrance();
     //this->close();
    }
}

void sterilization_type::on_back_btn_clicked()
{
    BUTTON_SOUND;
    this->close();
}


void sterilization_type::button_clicked_process(QMap<QString,int > &datamap,bool isvacuum)
{
    BUTTON_SOUND
    is_pulsing_vacuum=isvacuum;
    read_datamap( datamap,isvacuum);
    if(manage)
    {
        uint checktype;
        if(is_pulsing_vacuum) checktype=1;
        else checktype=2;
        MCD_Interface->set_datamap(&datamap,checktype);
        MCD_Interface->showFullScreen();

    }
    else
    {

        if( sterilization_protype==LIQUID_RPOTYPE )
        reference_f0=sterilization_time/60.0 * pow(10.0,(sterilization_temperature-121.0)/10.0);
        sr->entrance();
       //this->close();
    }
}
void sterilization_type::on_textiles_btn_clicked()
{
     sterilization_protype=TEXTITLES_PROTYPE;
     str_sterilization_type=trUtf8("织物类");

     button_clicked_process(textiles_datamap,true);
}

void sterilization_type::on_BDtest_btn_clicked()
{
   BUTTON_SOUND;
   is_pulsing_vacuum=true;
    if(manage && !whole_machine_parameter.bottom_exhaust_machine)
    {
       user_add_process(true);
      }
    else
    {
   sterilization_protype=BD_TEST_PROTYPE;
   str_sterilization_type=trUtf8("BD测试");
   read_datamap( textiles_datamap,true);
   sterilization_time=210;
   dry_times=1;
   sr->entrance();
  // this->close();
    }
}


void sterilization_type::on_metals_btn_clicked()
{

    sterilization_protype=METALS_PROTYPE;
    str_sterilization_type=trUtf8("器械类");
    button_clicked_process(metals_datamap,true);
}

void sterilization_type::on_Bottom_Exhaust_btn_clicked()
{

     sterilization_protype=BOTTOM_EXHAUST_PROTYPE;
     str_sterilization_type=trUtf8("下排式");
     button_clicked_process(bottom_datamap,false);

}



void sterilization_type::on_liquid_btn_clicked()
{

        sterilization_protype=LIQUID_RPOTYPE;
        str_sterilization_type=trUtf8("液体类");
        button_clicked_process(liquid_datamap,false);


}


void sterilization_type::read_datamap(  QMap<QString,int > &datamap,bool isvacuum)
{//QMap 查找为空后添加一个空值映射
    sterilization_temperature=datamap[trUtf8("灭菌温度")];
    sterilization_time=       datamap[trUtf8("灭菌时间")];
    prebalance_time=          datamap[trUtf8("预平衡时间")];
    heat_delay_time=          datamap[trUtf8("加热延时")];

    if(isvacuum)
    {

    pulsing_times=            datamap[trUtf8("脉动次数")];
    pulsing_lowest_pressure=  datamap[trUtf8("脉动压力下限")];
    pulsing_highest_pressure= datamap[trUtf8("脉动压力上限")];
    dry_pressure=             datamap[trUtf8("干燥压力")];
    dry_keep_time=            datamap[trUtf8("干燥时间")];
    dry_times=                datamap[trUtf8("干燥次数")];
    datamap.remove(trUtf8("排冷次数"));
    datamap.remove(trUtf8("排冷压力"));

    }
    else
    {
        cold_exhaust_times=datamap[trUtf8("排冷次数")];
        cold_exhaust_pressure=datamap[trUtf8("排冷压力")];
        datamap.remove(trUtf8("脉动次数"));
        datamap.remove(trUtf8("脉动压力下限"));
        datamap.remove(trUtf8("脉动压力上限"));
        datamap.remove(trUtf8("干燥次数"));
        datamap.remove(trUtf8("干燥压力"));
        datamap.remove(trUtf8("干燥时间"));
    }
}


void sterilization_type::user_add_process(bool is_pulsing_vacuum)
{
     if(whole_machine_parameter.user_defined<3)
   {
         if( Affirm("确认添加自定义程序？"))
     {

       switch( ++ whole_machine_parameter.user_defined)
       {
       case 1:whole_machine_parameter.user_defined_1=is_pulsing_vacuum;
           break;
       case 2:whole_machine_parameter.user_defined_2=is_pulsing_vacuum;
           break;
       case 3:whole_machine_parameter.user_defined_3=is_pulsing_vacuum;
           break;
       defaut:break;
       }
      config_changed=true;
      user_defined_interface_process();
         }
   }
   else
     {
       if( Affirm("请先删除后再重新添加！"))
       {
       whole_machine_parameter.user_defined=0;
       ui->user_define_btn_1->hide();
       ui->user_define_btn_2->hide();
       ui->user_define_btn_3->hide();
       config_changed=true;
       }
   }
}


void sterilization_type::show_user_define_botton_text(uint btn_num, bool &user_define_n)
{

    QPushButton * btn;
    QString button_text;

      switch(btn_num)
    {
      case 1: btn=ui->user_define_btn_1;
          read_datamap( user_define1_datamap,whole_machine_parameter.user_defined_1);
          break;
      case 2: btn=ui->user_define_btn_2;
          read_datamap( user_define2_datamap,whole_machine_parameter.user_defined_2);
          break;
      case 3: btn=ui->user_define_btn_3;
           read_datamap( user_define3_datamap,whole_machine_parameter.user_defined_3);
          break;
      default:break;
    }



    if( user_define_n)
   {
        button_text=trUtf8("自定义")+QString::number(btn_num)+trUtf8("\n真空");
        is_pulsing_vacuum=true;
    }
    else
    {       
        button_text=trUtf8("自定义")+QString::number(btn_num)+ trUtf8("\n下排");
        is_pulsing_vacuum=false;
    }

    button_text+= QString::number(sterilization_temperature) +trUtf8("℃ \n")+QString::number(sterilization_time) +"s";
     btn->setText(button_text);

}

void sterilization_type::user_defined_interface_process()
{

    show_user_define_botton_text( 1,whole_machine_parameter.user_defined_1);
    show_user_define_botton_text( 2,whole_machine_parameter.user_defined_2);
    show_user_define_botton_text( 3,whole_machine_parameter.user_defined_3);

    switch( whole_machine_parameter.user_defined  )
    {
    case 0:
        ui->user_define_btn_1->hide();
        ui->user_define_btn_2->hide();
        ui->user_define_btn_3->hide();
        break;
    case 1:
         ui->user_define_btn_1->show();
         ui->user_define_btn_2->hide();
         ui->user_define_btn_3->hide();
        break;
    case 2:
        ui->user_define_btn_1->show();
        ui->user_define_btn_2->show();
        ui->user_define_btn_3->hide();
        break;
    case 3:
         ui->user_define_btn_1->show();
         ui->user_define_btn_2->show();
         ui->user_define_btn_3->show();
        break;
    defaut:  break;
    }
}

 void sterilization_type::enter()
{
      if(manage){
          ui->BDtest_btn->setText(trUtf8("脉动+"));
          ui->manual_operation_btn->setText(trUtf8("下排式+"));
      }
      else{
          ui->BDtest_btn->setText(trUtf8("BD实验"));
          ui->manual_operation_btn->setText(trUtf8("手动"));
      }

      if( whole_machine_parameter.bottom_exhaust_machine)
      {
             ui->textiles_btn->hide();
             ui->metals_btn->hide();
             ui->BDtest_btn->hide();
      }
      else
      {
              ui->textiles_btn->show();
              ui->metals_btn->show();
              ui->BDtest_btn->show();
          //ui->dry_btn->show();

      }

       data_predisplay();
       if( whole_machine_parameter.user_defined )user_defined_interface_process();
       showFullScreen();
}

 void sterilization_type::user_define_process( QMap<QString,int >& datamap,bool is_pulsing_vacuum)
 {

        BUTTON_SOUND;
        read_datamap(datamap,is_pulsing_vacuum);
        uint checktype;
        if(is_pulsing_vacuum) checktype=1;
        else checktype=2;

        if(manage)
    {
          MCD_Interface->set_datamap(&datamap,checktype);
          MCD_Interface->showFullScreen();

    }
      else
    {

          if( is_pulsing_vacuum)
          {

              str_sterilization_type=trUtf8("自定义脉动真空");
            }
          else{
              str_sterilization_type=trUtf8("自定义下排式");
              }
          sr->entrance();
          //this->close();
      }


 }

   void sterilization_type::on_user_define_btn_1_clicked()
   {
      sterilization_protype=USER_DEFINE_1;
      show_user_define_botton_text( 1,whole_machine_parameter.user_defined_1);
      user_define_process( user_define1_datamap,whole_machine_parameter.user_defined_1);
   }

   void sterilization_type::on_user_define_btn_2_clicked()
   {
       sterilization_protype=USER_DEFINE_2;
       show_user_define_botton_text( 2,whole_machine_parameter.user_defined_2);
       user_define_process(user_define2_datamap,whole_machine_parameter.user_defined_2);

   }

   void sterilization_type::on_user_define_btn_3_clicked()
   {
        sterilization_protype=USER_DEFINE_3;
        show_user_define_botton_text( 3,whole_machine_parameter.user_defined_3);
        user_define_process(user_define3_datamap,whole_machine_parameter.user_defined_3);

   }

   void sterilization_type::data_predisplay()
   {
       static QString str;

       if(!whole_machine_parameter.bottom_exhaust_machine)
       {
      read_datamap( textiles_datamap,true);
      str=trUtf8("织物类")+"\n"+ QString::number(sterilization_temperature)
              +trUtf8("℃ \n")+QString::number(sterilization_time) +"s";
      ui->textiles_btn->setText(str);
      if(!manage)
      {
      str=trUtf8("BD测试")+"\n"+ QString::number(sterilization_temperature)
              +trUtf8("℃ \n") +"210s";
      ui->BDtest_btn->setText(str);
      }
      read_datamap( metals_datamap,true);
       str=trUtf8("器械类")+"\n"+ QString::number(sterilization_temperature)
              +trUtf8("℃ \n")+QString::number(sterilization_time) +"s";
      ui->metals_btn->setText(str);

      }

      read_datamap( bottom_datamap,false);
       str=trUtf8("下排式")+"\n"+ QString::number(sterilization_temperature)
              +trUtf8("℃ \n")+QString::number(sterilization_time) +"s";
      ui->Bottom_Exhaust_btn->setText(str);
      read_datamap( liquid_datamap,false);
       str=trUtf8("液体类")+"\n"+ QString::number(sterilization_temperature)
              +trUtf8("℃ \n")+QString::number(sterilization_time) +"s";
      ui->liquid_btn->setText(str);
   }

   void sterilization_type::sterilization_parameters_edited()
   {

       if(manage) //灭绝过程修改传感器参数后如果重新读取参数出错
      {
       data_predisplay();
       user_defined_interface_process();
      }
   }
